<?php
/**
 * Created by PhpStorm.
 * User: krystal
 * Date: 2019/3/22
 * Time: 15:35
 */

namespace app\common\model;

use think\Cache;

/**
 * 订单支付方式模型
 * Class ZhunlePay
 * @package app\common\model
 */
class ZhunlePay extends Base
{
    protected $table = 'wechat_backup_07_18_14_43_00.zhunle_pay';

    const ALL_PAY_IDS = 'all:pay:ids';
    const PAY_INFO = 'pay:info:';
    const CACHE_TIME = 86400 * 7;

    /**
     * 获取所有支付方式相关信息
     * @return array
     */
    public function getAllPayInfo()
    {
        $PayIds = false;
        $data_list = [];

        if ($this->cache_on >= 1) {
            $PayIds = Cache::get(self::ALL_PAY_IDS);
        }

        if ($PayIds === false) {
            try {
                $data = $this->column('id');

                if ($data) {
                    $PayIds = implode(',', $data);
                }
                if ($this->cache_on >= 1 && $PayIds) {
                    Cache::set(self::ALL_PAY_IDS, $PayIds, self::CACHE_TIME);
                }

            } catch (\Exception $e) {
                $this->error = '获取支付方式id失败';
                return false;
            }
        }

        if ($PayIds) {
            $PayIds = explode(',', $PayIds);
        }

        if (!empty($PayIds)) {
            $ac_ids = '';
            $temp = [];

            // 获取缓存的课程章节内容,根据课程章节id
            foreach ($PayIds as $ac_id) {
                if ($this->cache_on >= 1) {
                    // 根据课程章节id 获取课程章节内容
                    $temp[$ac_id] = Cache::get(self::PAY_INFO . $ac_id);
                } else {
                    // 缓存未开启时，默认从数据库获取
                    $temp[$ac_id] = false;
                }
                // 课程章节内容未缓存，等待从数据库中获取
                if ($temp[$ac_id] === false && $ac_id) {
                    $ac_ids .= $ac_id . ',';
                }
            }


            // 从数据库获取课程章节内容
            if ($ac_ids != '' || strlen($ac_ids) > 0) {
                //去掉结尾的 ，
                $ac_ids = substr($ac_ids, 0, strlen($ac_ids) - 1);

                $where = ['status' => 1,];

                try {
                    $data = $this
                        ->field('id,is_default,name,icon,expire_time')
                        ->where($where)
                        ->where('id in(' . $ac_ids . ')')
                        ->order('sort DESC,create_time DESC')
                        ->select();

                    $course = json_decode(json_encode($data, true), true);
                } catch (\Exception $e) {
                    $this->error = '获取活动信息失败.';
                    return false;
                }

                if (!empty($course)) {
                    foreach ($course as $c) {
                        // 将课程章节内容缓存
                        if ($this->cache_on >= 1) {
                            Cache::set(self::PAY_INFO . $c['id'], $c, self::CACHE_TIME);
                        }
                        // 将数据库查出来的内容 写入到数组中为空的部分
                        if ($c) {
                            $temp[$c['id']] = $c;
                        }
                    }
                }
            }
            foreach ($temp as $item) {
                if ($item) {
                    $data_list[] = $item;
                }
            }
        }

        return $data_list;

    }


    /**
     * 获取具体某个支付方式信息
     * @param array $map
     * @return array|bool|mixed|\PDOStatement|string|\think\Model
     */
    public function getPayInfo($map = [])
    {
        $id = $map['id'];

        $data = false;

        if ($this->cache_on >= 1) {
            $data = Cache::get(self::PAY_INFO);
        }

        if ($data === false) {
            $where = ['id' => $id, 'status' => 1];
            try {
                $data = $this
                    ->field('id,is_default,name,icon,expire_time')
                    ->where($where)
                    ->findOrFail();

                $data = json_decode(json_encode($data, true), true);
                if ($this->cache_on >= 1) {
                    Cache::set(self::PAY_INFO . $id, $data, self::CACHE_TIME);
                }
            } catch (\Exception $e) {
                $this->error = '获取支付方式信息失败';
                return false;
            }
        }

        return $data;
    }

}
